package com.minimalist.lindi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.lindi.po.LindiContractTemplate;
import com.minimalist.common.entity.lindi.vo.contracttemplate.LindiContractTemplateQueryVO;
import com.minimalist.common.entity.lindi.vo.contracttemplate.LindiContractTemplateVO;
import com.minimalist.common.feign.client.basic.FileFeignClient;
import com.minimalist.lindi.manager.ContractTemplateManager;
import com.minimalist.lindi.mapper.LindiContractTemplateMapper;
import com.minimalist.lindi.service.LindiContractTemplateService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 林地指标平台_合同模板表 服务层实现。
 *
 * @author 11987
 * @since 2025-05-31
 */
@Service
public class LindiContractTemplateServiceImpl extends ServiceImpl<LindiContractTemplateMapper, LindiContractTemplate> implements LindiContractTemplateService{

    @Autowired
    private LindiContractTemplateMapper mapper;

    @Autowired
    private ContractTemplateManager contractTemplateManager;

    @Autowired
    private FileFeignClient fileFeignClient;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addContractTemplate(LindiContractTemplateVO contractTemplateVO){
        contractTemplateManager.insertContractTemplate(contractTemplateVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteContractTemplateByContractTemplateId(Long contractTemplateId){
        contractTemplateManager.deleteContractTemplate(contractTemplateId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateContractTemplateByContractTemplateId(
            LindiContractTemplateVO contractTemplateVO){
        contractTemplateManager.updateContractTemplate(contractTemplateVO);
    }

    @Override
    public PageResp<LindiContractTemplateVO> getPageContractTemplateList(
            LindiContractTemplateQueryVO queryVO){
        // 分页查询
        Page<LindiContractTemplate> templatePage = mapper
                .selectPageContractTemplateList(queryVO);
        // 数据转换
        List<LindiContractTemplateVO> voList = BeanUtil.copyToList(
                templatePage.getRecords(), LindiContractTemplateVO.class);
        return new PageResp<>(voList, templatePage.getTotalRow());
    }

    @Override
    public LindiContractTemplateVO getContractTemplateByContractTemplateId(
            Long contractTemplateId){
        return contractTemplateManager.getContractTemplate(contractTemplateId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteContractTemplateFileByFileId(Long fileId){
        contractTemplateManager.deleteContractTemplateFileByFileId(fileId);
    }
}
